package com.codeduck.task.scheduled;

import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 基于 JDK ScheduledExecutorService 方式,相对的比 Timer 要好
 *
 * @author: <a href="mailto:codeduck@163.com">Codeduck</a>
 * @createDate: 2021-12-26
 */
public class ScheduledExecutorServiceDemo {
    public static void main(String[] args) {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);
        /**
         * 参数：1、具体执行的任务
         *      2、首次执行的延时时间
         *      3、任务执行间隔
         *      4、间隔时间单位
         */
        executorService.scheduleAtFixedRate(() ->
                        System.out.println(Thread.currentThread().getName() + " 执行任务的时间： " + LocalDateTime.now()),
                0,3, TimeUnit.SECONDS);
    }
}
